global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/spillover_stocks.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {


* Set maxvar
clear
set maxvar 32767

* Load country list
do ${code_dir}/config/country_list.do


*This file build stocks for all technologies. Not at the Firm (BvD) level (that happens in depvars.do) but at the country level. Otherwise same procedure. 
*Country Stock is defined as all patents first filed in a country in a given year (proportianal to unique inventor homecountries) and 0.85 of the stock form the previous year.
*we use these countrystocks to generate spillovers based on the inventor homecountry shares of the firms. 


**************************************************************************************************************************************
* Country level invention counts
**************************************************************************************************************************************


foreach tech in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa {
	
	if "`tech'" == "tfa" {
		local techfile ${dataset_dir}/patent_list/pats_`tech'.dta
	}
	else {
		local techfile ${dataset_dir}/patent_list/`tech'_patents.dta
	}
	*load data
	use `techfile', clear

	*merge in the (completed) inventors (some are imputed)
	mmerge appln_id using ${commondata_dir}/patstat_2018b/appln_inventor_country_nomissing.dta, unmatched(none)

	*corret GER/GDR and RUS/USSR
	do ${code_dir}/config/auth_map.do invt_country

	*merge in the earliest application year in a patent family and the family identifier
	mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id fam_earliest_appln_year)

	*dropping appln id and then duplicates makes sure we only have unique homecountries
	drop appln_id _m
	duplicates drop
	ren fam_earliest_appln_year year 
	drop if year == 9999
	gen byte x = 1

	*count the number of unique filing countries for each invention and split equally between them
	bysort docdb : egen nb_invt_ctries = sum(x)
	gen inv_nb_invt_ctries = 1/nb_invt_ctries
	drop nb_invt_ctries

	*merge biadic information
	mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/docdb_families2.dta, unmatched(master) ukeep(biadic_D)
	ren biadic_D bia
	drop _m x

	*count the number of attributed inventions for a country in a given year
	gen byte x=1
	bysort invt_country year : egen nb_inv = sum(inv_nb_invt*x)
	bysort invt_country year : egen nb_bia = sum(inv_nb_invt*bia*x)


	keep year invt_country nb_*
	duplicates drop
	sort invt year

	*labeling


	save ${dataset_dir}/spillovers/ctry_inventions_count_`tech'.dta, replace
}

* country year invention counts in all technologies (same as above but for all technologies)

*load inventors
use ${commondata_dir}/patstat_2018b/appln_inventor_country_nomissing.dta, clear
*correct GER/GDR and RUS/USSR
do ${code_dir}/config/auth_map.do invt_country

*merge family identifier and first filing year
mmerge appln_id using ${commondata_dir}/patstat_2018b/family_info.dta, unmatched(master) ukeep(docdb_family_id fam_earliest_appln_year)

*drop duplicate homecountries per patent family
drop appln_id _m
duplicates drop
ren fam_earliest_appln_year year
drop if year == 9999

*count unique filing countries per invention
gen byte x=1
bysort docdb : egen nb_invt_ctries = sum(x)
gen inv_nb_invt_ctries = 1/nb_invt_ctries
drop nb_invt_ctries x

*merge biadic information
mmerge docdb_family_id using ${dataset_dir}/patstat_orbis/docdb_families2.dta, unmatched(master) ukeep(biadic_D)
ren biadic_D bia
drop _m
*count how many inventors from that country in that year
bysort invt_country year : egen nb_inv = sum(inv_nb_invt)   
bysort invt_country year : egen nb_bia = sum(inv_nb_invt*bia)

keep year invt_country  nb_*
duplicates drop
sort invt year

*labeling


save ${dataset_dir}/spillovers/ctry_inventions_count_alltechs.dta, replace

**************************************************************************************************************************************
* average shares (for companies that have 0 patent in pre-sample period)
**************************************************************************************************************************************

foreach tech in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa alltechs {
	use  ${dataset_dir}/spillovers/ctry_inventions_count_`tech'.dta, clear
	drop if year == 9999
	drop if year > 1995
	drop if year < 1995-9

	*generate avergae share of biadic patents
	collapse (sum) nb_*, by(invt_country)
	egen total_bia = sum(nb_bia)
	gen share_WD_total_bia_ = nb_bia / total_bia
	drop nb* total*
	gen x = 1
	reshape wide share_*, i(x) j(invt_country) string
	keep sh*
	save ${dataset_dir}/weights/shares_INV_allctries_`tech'_1995.dta, replace
}


**************************************************************************************************************************************
* Country level invention stocks by tech
**************************************************************************************************************************************

*load cpmplete set and complete panel
use ${dataset_dir}/spillovers/ctry_inventions_count_alltechs.dta, clear
keep if year > 1899
fillin invt year

ren nb_bia bia_alltechs
replace bia_alltechs=0 if bia_alltechs==.

drop _f

*load more specific techs
foreach tech in auto80 auto90 auto95 autoX95 autonol95 pauto90 pauto95 pauto90_rm6 CNC80 CNC90 CNC95 robo80 robo90 robo95 autm80 autm90 autm95 tfa {
	mmerge invt year using ${dataset_dir}/spillovers/ctry_inventions_count_`tech'.dta, unmatched(master)
	local any bia_alltechs
	ren nb_bia bia_`tech'
	replace bia_`tech'=0 if bia_`tech'==.

	*use the alltechs count/any to get patent counts of patents NOT in that technology
	gen bia_N_`tech' = `any' - bia_`tech'
	replace bia_N_`tech'=0 if bia_N_`tech'==.
}
drop _m

*generate stocks
qui ds, has(type float)
foreach pt in `r(varlist)' {
	gen k`pt'=`pt'
	bysort invt (year): replace k`pt'=k`pt'+0.85* k`pt'[_n-1] if _n>1
}

keep if year > 1969

*labeling

*save for other uses in long format
compress
save ${dataset_dir}/spillovers/ctry_inventions_stocks.dta, replace

*keep only the countries, year and stocks
keep invt_country year k*
drop *_alltechs
keep year invt_country kbia_*

*reshape to wide format
foreach pt of varlist k* {
	ren `pt' `pt'_
}
reshape wide k*, i(year) j(invt_country) string
compress
save ${dataset_dir}/spillovers/ctry_inventions_stocks_wide_bia.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat